package com.ruoyi.contract.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.contract.domain.IceFlow;
import com.ruoyi.contract.service.IceFlowService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping("/contract/ice-flow")
public class IceFlowController extends BaseController {

    @Autowired
    IceFlowService iceFlowService;

    @GetMapping("/list")
    public TableDataInfo list(IceFlow flow){
        startPage();
        QueryWrapper<IceFlow> queryWrapper = new QueryWrapper<>();

        queryWrapper.lambda()
                .like(StringUtils.isNotEmpty(flow.getFlowName()),IceFlow::getFlowName,flow.getFlowName())
                .like(StringUtils.isNotEmpty(flow.getDatabaseName()),IceFlow::getDatabaseName,flow.getDatabaseName())
                .orderByDesc(IceFlow::getId);

        List<IceFlow> list = iceFlowService.list(queryWrapper);
        return getDataTable(list);
    }

    @GetMapping("/getOne/{id}")
    public AjaxResult getOne(@PathVariable Long id){
        return AjaxResult.success(iceFlowService.getById(id));
    }

    @GetMapping("/label")
    public AjaxResult label(){
        return AjaxResult.success(iceFlowService.list());
    }

    @PostMapping("/save")
    @PreAuthorize("@ss.hasPermi('contract:ice-flow:add')")
    @Log(title = "新增判断节点", businessType = BusinessType.INSERT)
    public AjaxResult save(@RequestBody IceFlow flow){

        try {
            return iceFlowService.insert(flow);
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }

    }

    @PutMapping("/update")
    @PreAuthorize("@ss.hasPermi('contract:ice-flow:edit')")
    @Log(title = "修改判断节点", businessType = BusinessType.UPDATE)
    public AjaxResult update(@RequestBody IceFlow flow){

        try {
            return iceFlowService.update(flow);
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }

    }

    @DeleteMapping("/delete/{id}")
    @PreAuthorize("@ss.hasPermi('contract:ice-flow:remove')")
    @Log(title = "删除判断节点", businessType = BusinessType.DELETE)
    public AjaxResult delete(@PathVariable Long[] id){
        return iceFlowService.removeByIds(Arrays.asList(id))?AjaxResult.success():AjaxResult.error();
    }

}
